/**
 * 
 */
package com.manolo.component.desing;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.manolo.framework.connection.client.ClientMultiplayer;
import com.manolo.framework.connection.entity.Message;
import com.manolo.framework.connection.entity.Player;
import com.manolo.framework.deck.Card;
import com.manolo.framework.deck.Deck;
import com.manolo.framework.deck.SUIT;
import com.manolo.framework.listener.ReciveDateListener;
import com.manolo.framework.listener.dispatcher.DateDispatcher;
import com.manolo.framework.listener.dispatcher.ReciveDateDispatcher;
import com.manolo.framework.listener.event.DateEvent;
import com.manolo.framework.util.Util;

/**
 * @author gustavo
 * 
 */
public class ScreenMultiPlayer extends Canvas implements Runnable, KeyListener,
		ReciveDateListener {

	private ClientMultiplayer client;
	private Player player;
	// Loop game
	Thread gameLoop;
	// Use this with backgrouf buffer - process next image that will be show
	BufferedImage backBuffer;
	// Class responsable to draw image at screen
	Graphics2D g2D;
	// Cria a transformação identidade(0,0)
	AffineTransform identity = new AffineTransform();
	Image image;
	BufferedImage imageSuit;

	private List<CardScr> deck;

	private Color color;

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public ScreenMultiPlayer() {
		player = new Player();
		color = Color.BLACK;

		ReciveDateDispatcher.getInstance().addDateListener(this);
		
		this.client = new ClientMultiplayer("localhost", 4555, player);

		Thread playerThread = new Thread(client);
		playerThread.start();

	}

	/*
	 * Method initialization screen
	 */
	public void init() {
		setSize(800, 600);
		// Create buffer
		backBuffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
		deck = new ArrayList<CardScr>();
		g2D = backBuffer.createGraphics();

		g2D.setTransform(identity);
		// Backgroud clear
		g2D.setPaint(color);
		g2D.fillRect(0, 0, getSize().width, getSize().height);

		g2D.setColor(Color.WHITE);
		g2D.drawString("Pressiona a tecla Enter ou ESPACO :", 300, 10);

		try {
			imageSuit = Util.loadImage("SUITS.png");
			image = imageSuit.getScaledInstance(10, 10, 10);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.err.println("Error to load image");
		}

		// getStartDeck();

		addKeyListener(this);
	}

	@Override
	public void update(Graphics g) {

		g2D.setTransform(identity);
		// Backgroud clear
		g2D.setPaint(color);
		g2D.fillRect(0, 0, getSize().width, getSize().height);

		g2D.setColor(Color.WHITE);
		g2D.drawString("Pressiona a tecla Enter ou ESPACO :", 300, 300);

		g2D.setTransform(identity);
		// Backgroud clear
		g2D.setPaint(color);
		g2D.fillRect(0, 0, getSize().width, getSize().height);

		for (CardScr card : deck) {
			drawCard(card);
		}

		// Screen update
		paint(g);
	}

	@Override
	public void paint(Graphics g) {
		g.drawImage(backBuffer, 0, 0, this);
	}

	public void start() {
		gameLoop = new Thread(this);
		gameLoop.start();
	}

	/*
	 * Game life cycle
	 */
	@Override
	public void run() {
		Thread t = Thread.currentThread();

		
		while (t == gameLoop) {
			try {
				gameUpdate();
				// target framerate is 50 fps
				
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			repaint();
		}

	}

	private void gameUpdate() {
		if (player.getCards() != null && player.getCards().size() > 0) {
			deck.clear();

			int area = 160;
			int i = 0;
			for (Card card : player.getCards()) {
				int areaTmp = i * area;
				CardScr cardTmp = new CardScr(card);
				cardTmp.setX(areaTmp);
				cardTmp.setY(area * 2);
				this.deck.add(cardTmp);
				i++;
			}
		}

	}

	private void drawCard(CardScr card) {
		g2D.setTransform(identity);
		g2D.translate(card.getX(), card.getY());
		g2D.setColor(Color.WHITE);
		g2D.fill(card.getShape());

		g2D.setColor(Color.BLACK);
		g2D.drawRect(0, 0, 120, 160);
		// Draw number
		g2D.drawString(card.getCard().getNumber().toString(), 10, 20);
		// Draw Suits

		if (card.getCard().getSuit().equals(SUIT.HEARTS)) {
			g2D.drawImage(imageSuit, 0, 0, 120, 150, 150, 0, 300, 150, null);
			// g2D.drawImage(imageSuit, 0, 0, 120, 150, 150, 0, 300, 150, null);
			g2D.drawString("HEARTS", 10, 155);
		} else if (card.getCard().getSuit().equals(SUIT.SPADES)) {
			g2D.drawImage(imageSuit, 0, 0, 120, 150, 0, 0, 150, 150, null);
			g2D.drawString("SPADES", 10, 155);
		} else if (card.getCard().getSuit().equals(SUIT.DIAMONDS)) {
			g2D.drawImage(imageSuit, 0, 0, 120, 150, 0, 150, 150, 300, null);
			g2D.drawString("DIAMONDS", 10, 155);
		} else if (card.getCard().getSuit().equals(SUIT.CLUBS)) {
			g2D.drawImage(imageSuit, 0, 0, 120, 150, 160, 150, 330, 300, null);
			g2D.drawString("CLUBS", 10, 155);
		}

	}

	@Override
	public void keyPressed(KeyEvent k) {
		int keyCode = k.getKeyCode();

		// if ((keyCode == 77 || keyCode == 16)) {
		// reciveMessage = true;
		// }
		// if (keyCode == KeyEvent.VK_LEFT || keyCode == 37) {
		// ship.moveShipToLeft();
		// }
		// if (keyCode == KeyEvent.VK_RIGHT || keyCode == 39) {
		// ship.moveShipToRight();
		// }
		// if (keyCode == KeyEvent.VK_UP || keyCode == 38) {
		// ship.moveShipToUp();
		// }
		// if (keyCode == KeyEvent.VK_DOWN || keyCode == 40) {
		// ship.moveShipToDown();
		// }
		if ((keyCode == KeyEvent.VK_SPACE) || (keyCode == 32)
				|| (keyCode == KeyEvent.VK_ENTER)
				|| (keyCode == KeyEvent.VK_CONTROL) || (keyCode == 17)) {
			deck.clear();
			System.out.println("Press enter");
			DateEvent event = new DateEvent();

			DateDispatcher.getInstance().dispatchDateEvent(event);
			player.setName("Gustavo");
			// player.notifyAll();
		}

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moveAction(DateEvent event) {
		if (event.getCards().size() > 0) {
			player.getCards().clear();
			player.getCards().addAll(event.getCards());

			DateEvent event2 = new DateEvent();
			event.setId(player.getId());
			Message msg = new Message();
			msg.setContent("Tenho 3 cartas");
			event2.getMessages().add(msg);

			//DateDispatcher.getInstance().dispatchDateEvent(event2);
			
		}

	}

}
